package com.hnucm.shopapp.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.hnucm.shopapp.R;
import com.hnucm.shopapp.bean.User;
import com.hnucm.shopapp.ui.base.BaseActivity;
import com.hnucm.shopapp.ui.register.RegisterActivity;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class LoginActivity extends BaseActivity implements LoginContract.ILoginView, View.OnClickListener {
    private Button login;
    private EditText userAccount, userPassword;
    private TextView register, forgetPassword;
    private LoginPresenter loginPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        setTheme(R.style.Theme_ShopApp);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initViews() {
        login = find(R.id.btnLoginLogin);
        userPassword = find(R.id.edtextLoginPasswd);
        userAccount = find(R.id.edtextLoginUserAccount);
        loginPresenter = new LoginPresenter(this);
        register = find(R.id.textLoginRegister);
        forgetPassword = find(R.id.textLoginForget);
        //监听事件
        login.setOnClickListener(this);
        register.setOnClickListener(this);
        forgetPassword.setOnClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void getUserByAccountSuccess(List<User> userList) {
        if (userList.size() == 0) {
            Toast.makeText(LoginActivity.this, "账号不存在", Toast.LENGTH_SHORT).show();
        } else if (userList.get(0).getUserPassword().equals(userPassword.getText().toString())) {
            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public void getUserByAccountError(Throwable throwable) {
        Toast.makeText(LoginActivity.this, "服务器出错啦", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnLoginLogin:
                HashMap<String, Object> map = new HashMap<>();
                map.put("userAccount", userAccount.getText().toString());
                RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), new JSONObject(map).toString());
                loginPresenter.getUserByAccount(requestBody);
                break;
            case R.id.textLoginRegister:
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.textLoginForget:
                Toast.makeText(LoginActivity.this, "页面正在开发中", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }
}
